package c.h.a.j0;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SearchView.l;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.z;
import c.h.a.b0.m;
import c.h.a.b0.o;
import c.h.a.m0.h;
import c.h.a.m0.n;
import c.h.a.x.p;
import c.h.a.x.q;
import c.h.a.x.r;
import com.liuzh.deviceinfo.DeviceInfoApp;
import com.liuzh.deviceinfo.appinfo.AppInfoActivity;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* compiled from: TabApps */
public class i0 extends h0 {
    public int W = 0;
    public View X;
    public final List<c.h.a.d0.a> Y = new ArrayList();
    public final List<c.h.a.d0.a> Z = new ArrayList();
    public List<c.h.a.d0.a> b0 = new ArrayList();
    public RecyclerView c0;
    public e d0;
    public TextView e0;
    public c.h.a.d0.a f0;
    public f g0;
    public Context h0;
    public SearchView i0;
    public boolean j0 = false;
    public final List<c.h.a.d0.a> k0 = new ArrayList();

    /* compiled from: TabApps */
    public class a implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
            if (!i0.this.m0()) {
                i0 i0Var = i0.this;
                i0Var.W = i;
                SearchView searchView = i0Var.i0;
                if (searchView == null || !i0Var.j0) {
                    i0Var.d0.a.b();
                    i0.this.t0();
                } else {
                    i0Var.x0(searchView.getQuery().toString());
                }
            }
        }

        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    /* compiled from: TabApps */
    public class f extends BroadcastReceiver {
        public final int a(List<c.h.a.d0.a> list, String str) {
            for (int size = list.size() - 1; size >= 0; size--) {
                c.h.a.d0.a aVar = (c.h.a.d0.a) list.get(size);
                if (aVar.c.equals(str)) {
                    list.remove(aVar);
                    return size;
                }
            }
            return -1;
        }

        public void onReceive(Context context, Intent intent) {
            if (!i0.this.m0()) {
                if ("android.intent.action.PACKAGE_REMOVED".equals(intent.getAction())) {
                    String dataString = intent.getDataString();
                    if (!TextUtils.isEmpty(dataString)) {
                        dataString = dataString.substring(8);
                        int a = a(i0.this.Y, dataString);
                        int a2 = a(i0.this.b0, dataString);
                        int a3 = a(i0.this.Z, dataString);
                        int a4 = a(i0.this.k0, dataString);
                        i0 i0Var = i0.this;
                        e eVar = i0Var.d0;
                        if (eVar != null) {
                            if ((i0Var.W == 2 ? 1 : null) != null) {
                                if (a3 >= 0) {
                                    eVar.e(a3);
                                }
                            } else if (i0Var.y0()) {
                                if (a2 >= 0) {
                                    i0.this.d0.e(a2);
                                }
                            } else if (!i0.this.z0()) {
                                i0 i0Var2 = i0.this;
                                if (i0Var2.j0 && a4 >= 0) {
                                    i0Var2.d0.e(a4);
                                }
                            } else if (a >= 0) {
                                i0.this.d0.e(a);
                            }
                            i0.this.t0();
                        }
                    }
                }
            }
        }
    }

    /* compiled from: TabApps */
    public class b implements l {
    }

    /* compiled from: TabApps */
    public class c implements c.h.a.x.r.a {
        public String a;
        public final /* synthetic */ com.liuzh.deviceinfo.appinfo.AppInfoActivity.f b;
        public final /* synthetic */ String c;

        public c(i0 i0Var, com.liuzh.deviceinfo.appinfo.AppInfoActivity.f fVar, String str) {
            this.b = fVar;
            this.c = str;
        }

        public void a() {
            this.b.m0(false, false);
            Toast.makeText(DeviceInfoApp.c, 2131689838, 0).show();
        }

        public void b(long j, long j2) {
            if (this.a == null) {
                this.a = Formatter.formatFileSize(DeviceInfoApp.c, j2);
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(Formatter.formatFileSize(DeviceInfoApp.c, j));
            stringBuilder.append("/");
            stringBuilder.append(this.a);
            String stringBuilder2 = stringBuilder.toString();
            Dialog dialog = this.b.e0;
            if (dialog != null && dialog.isShowing()) {
                this.b.s0(stringBuilder2);
            }
            if (j == j2) {
                this.b.r0(new File(r.a, this.c).getPath());
            }
        }

        public boolean stop() {
            return this.b.j0;
        }
    }

    /* compiled from: TabApps */
    public class d implements n {
        public final /* synthetic */ m a;
        public final /* synthetic */ String b;

        public d(m mVar, String str) {
            this.a = mVar;
            this.b = str;
        }

        public void a() {
            this.a.m0(false, false);
            Toast.makeText(DeviceInfoApp.c, 2131689838, 0).show();
        }

        public void onSuccess() {
            if (!h.q(i0.this.e())) {
                this.a.m0(false, false);
                String path = new File(r.c, this.b).getPath();
                b.b.c.f.a aVar = new b.b.c.f.a(i0.this.e());
                aVar.e(2131690257);
                Object[] objArr = new Object[]{path};
                aVar.a.f = i0.this.s().getString(2131689965, objArr);
                aVar.d(17039370, null);
                aVar.a.m = false;
                aVar.b(17039361, new b(path));
                aVar.g();
            }
        }
    }

    /* compiled from: TabApps */
    public class e extends androidx.recyclerview.widget.RecyclerView.e<a> {

        /* compiled from: TabApps */
        public class a extends z implements OnClickListener, OnLongClickListener {
            public TextView t;
            public TextView u;
            public TextView v;
            public ImageView w;

            public a(View view) {
                super(view);
                this.t = (TextView) view.findViewById(2131296753);
                this.u = (TextView) view.findViewById(2131296798);
                this.v = (TextView) view.findViewById(2131297327);
                this.w = (ImageView) view.findViewById(2131296628);
                this.a.setOnClickListener(this);
                this.a.setOnLongClickListener(this);
            }

            public void onClick(View view) {
                c.h.a.d0.a s0 = i0.s0(i0.this, e());
                if (s0 != null) {
                    AppInfoActivity.F(i0.this.Z(), s0.c);
                }
            }

            public boolean onLongClick(View view) {
                c.h.a.d0.a s0 = i0.s0(i0.this, e());
                if (s0 == null) {
                    return false;
                }
                i0 i0Var = i0.this;
                i0Var.f0 = s0;
                b.b.c.f.a aVar = new b.b.c.f.a(i0Var.Z());
                c cVar = new c(i0.this);
                androidx.appcompat.app.AlertController.b bVar = aVar.a;
                bVar.p = bVar.a.getResources().getTextArray(2130903043);
                aVar.a.r = cVar;
                aVar.g();
                return true;
            }
        }

        public int a() {
            return i0.this.v0().size();
        }

        @SuppressLint({"SetTextI18n"})
        public void f(z zVar, int i) {
            a aVar = (a) zVar;
            List v0 = i0.this.v0();
            if (i >= 0 && i < v0.size()) {
                c.h.a.d0.a s0 = i0.s0(i0.this, i);
                if (s0 != null) {
                    aVar.t.setText(s0.a);
                    aVar.u.setText(s0.c);
                    aVar.v.setText(s0.b);
                    Drawable drawable = s0.d;
                    if (drawable == null) {
                        drawable = null;
                        try {
                            drawable = s0.g.loadIcon(DeviceInfoApp.c.b());
                        } catch (OutOfMemoryError | SecurityException unused) {
                        }
                        if (drawable == null) {
                            aVar.w.setImageResource(17629184);
                            return;
                        }
                        s0.d = drawable;
                        aVar.w.setImageDrawable(drawable);
                        return;
                    }
                    aVar.w.setImageDrawable(drawable);
                }
            }
        }

        public z g(ViewGroup viewGroup, int i) {
            return new a(LayoutInflater.from(viewGroup.getContext()).inflate(2131492986, viewGroup, false));
        }
    }

    public static c.h.a.d0.a s0(i0 i0Var, int i) {
        return (c.h.a.d0.a) i0Var.v0().get(i);
    }

    public void D(Context context) {
        super.D(context);
        this.h0 = context;
    }

    public void E(Bundle bundle) {
        super.E(bundle);
        if (!this.C) {
            this.C = true;
            if (z() && !this.y) {
                this.s.q();
            }
        }
        this.g0 = new f();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.PACKAGE_REMOVED");
        intentFilter.addDataScheme("package");
        this.h0.registerReceiver(this.g0, intentFilter);
    }

    public void F(Menu menu, MenuInflater menuInflater) {
        MenuItem add = menu.add(0, 2, 1, 2131690281);
        add.setIcon(2131231062);
        if (this.i0 == null) {
            SearchView searchView = new SearchView(new b.b.g.c(this.h0, 2131755024));
            this.i0 = searchView;
            searchView.setIconifiedByDefault(true);
            this.i0.setIconified(true);
            this.i0.setQueryHint(x(2131689490));
            this.i0.setOnQueryTextListener(new b());
            this.i0.setOnCloseListener(new d(this));
        }
        add.setActionView(this.i0);
        add.setShowAsActionFlags(2);
    }

    public View G(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        if (this.X == null) {
            View inflate = layoutInflater.inflate(2131493048, viewGroup, false);
            this.X = inflate;
            this.c0 = (RecyclerView) inflate.findViewById(2131296829);
            this.e0 = (TextView) this.X.findViewById(2131297265);
            e eVar = new e();
            this.d0 = eVar;
            this.c0.setAdapter(eVar);
            ((Spinner) this.X.findViewById(2131296979)).setOnItemSelectedListener(new a());
            this.X.findViewById(2131296404).setOnClickListener(new f(this));
            new Thread(new e(this)).start();
        }
        return this.X;
    }

    public void H() {
        this.E = true;
        this.h0.unregisterReceiver(this.g0);
    }

    public void I() {
        SearchView searchView = this.i0;
        if (searchView != null) {
            searchView.B("", false);
            this.i0.setIconified(true);
        }
    }

    public void O(int i, String[] strArr, int[] iArr) {
        if (!h.q(e())) {
            if (!c.h.a.e0.a.f(iArr)) {
                String str = "android.permission.WRITE_EXTERNAL_STORAGE";
                if (i == 1) {
                    if (!c.h.a.e0.a.d(this, str)) {
                        o.q0(e(), 2131690076, 2131689826);
                    }
                } else if (i == 2 && !c.h.a.e0.a.d(this, str)) {
                    o.q0(e(), 2131690076, 2131690255);
                }
            } else if (i == 1) {
                u0();
            } else if (i == 2) {
                w0();
            }
        }
    }

    public boolean n0() {
        SearchView searchView = this.i0;
        if (searchView == null || searchView.Q) {
            return false;
        }
        searchView.B("", false);
        this.i0.setIconified(true);
        return true;
    }

    public String r0() {
        return DeviceInfoApp.c.getString(2131689620);
    }

    public final void t0() {
        this.e0.setText(String.valueOf(v0().size()));
    }

    public final void u0() {
        if (this.f0 != null) {
            com.liuzh.deviceinfo.appinfo.AppInfoActivity.f q0 = com.liuzh.deviceinfo.appinfo.AppInfoActivity.f.q0(h(), "0/0");
            c.h.a.d0.a aVar = this.f0;
            String a = r.a(aVar.c, aVar.e);
            c.h.a.m0.p.a.a(new q(this.f0.f, new c(this, q0, a), a));
        }
    }

    public final List<c.h.a.d0.a> v0() {
        if (this.j0) {
            return this.k0;
        }
        if (z0()) {
            return this.Y;
        }
        if (y0()) {
            return this.b0;
        }
        return this.Z;
    }

    public final void w0() {
        c.h.a.d0.a aVar = this.f0;
        if (aVar != null) {
            Drawable drawable = aVar.d;
            if (drawable != null) {
                m mVar = new m();
                mVar.o0(true);
                mVar.p0(h(), mVar.toString());
                c.h.a.d0.a aVar2 = this.f0;
                String b = r.b(aVar2.c, aVar2.e);
                c.h.a.m0.p.a.a(new p(drawable, new d(mVar, b), b));
            }
        }
    }

    public final void x0(String str) {
        this.k0.clear();
        int i = 0;
        if (TextUtils.isEmpty(str)) {
            this.j0 = false;
            this.d0.a.b();
            t0();
            return;
        }
        String str2;
        boolean z = true;
        this.j0 = true;
        List list;
        if (y0()) {
            list = this.b0;
        } else if (z0()) {
            list = this.Y;
        } else {
            if (this.W != 2) {
                z = false;
            }
            if (z) {
                list = this.Z;
            } else {
                t0();
                return;
            }
        }
        str = str.toLowerCase();
        StringBuilder stringBuilder = new StringBuilder();
        while (true) {
            str2 = ".*?";
            if (i >= str.length()) {
                break;
            }
            stringBuilder.append(str2);
            stringBuilder.append(str.charAt(i));
            i++;
        }
        stringBuilder.append(str2);
        LinkedList linkedList = new LinkedList();
        try {
            Pattern compile = Pattern.compile(stringBuilder.toString());
            for (c.h.a.d0.a aVar : list) {
                Matcher matcher = compile.matcher(aVar.a.toLowerCase());
                if (matcher.find()) {
                    linkedList.add(new c.h.a.g0.a(matcher.start(), matcher.group().length(), aVar));
                }
            }
        } catch (Exception unused) {
        }
        Collections.sort(linkedList, c.h.a.g0.b.a);
        Iterator it = linkedList.iterator();
        while (it.hasNext()) {
            this.k0.add(((c.h.a.g0.a) it.next()).c);
        }
        this.d0.a.b();
        t0();
    }

    public final boolean y0() {
        return this.W == 1;
    }

    public final boolean z0() {
        return this.W == 0;
    }
}
